package com.srit.core.ibatis.page;

/**
 * oracle 分页修改
 * 
 * @author guan
 * 
 */
public class OracleDialect implements Dialect {
	protected static final String SQL_END_DELIMITER = ";";

	@Override
	public String getOracleLimit(String sql, int offset, int limit) {
		if (offset == 1) {
			// offset = 0;
		}
		StringBuffer pageStr = new StringBuffer();
		pageStr.append("select * from ( select row_limit.*,rownum rownum_ from (");
		pageStr.append(this.trim(sql));
		pageStr.append(" ) row_limit where rownum <= ");
		pageStr.append(limit + offset);
		pageStr.append(" ) where rownum_ >");
		pageStr.append(offset);
		return pageStr.toString();
	}

	/**
	 * 去掉当前SQL 后分号
	 * 
	 * @param sql
	 * @return
	 */

	private String trim(String sql) {
		sql = sql.trim();
		if (sql.endsWith(SQL_END_DELIMITER)) {
			sql = sql.substring(0,
					sql.length() - 1 - SQL_END_DELIMITER.length());
		}
		return sql;

	}

}
